<?php
namespace app\index\service;
use think\Db;
use think\Cache;
use other\Paginator;
use app\index\model\Course;
use app\index\model\Specific;
use app\index\model\NavPackage;
use app\index\model\KnowUserLog;
use app\index\model\KnowUserVip;
use app\index\model\KnowUserType;
use app\index\service\VipService;
use app\index\model\KnowUserRecord;
use app\index\model\KnowUserQuestion;



class CourseService
{
	public function disposeCourse($topData, $sData, $data, $field = '')
	{
		$data = self::disposeCourseType($data, $field);

		$sData = self::disposeCourseType($sData, $field);

		$user = user();

		foreach($topData as $key => &$v)
		{
			$v['points'] = $data[$v[$field]] ? returnCourseSumField($data[$v[$field]]) : 0;

			$v['s'] = $sData[$v[$field]];

			$userLogs = KnowUserLog::getUserCount([$field => $v[$field]], 'column', 'course.points');

			$v['userPoints'] = $user ? array_sum($userLogs) : 0;
		}

		return $topData;
	}


	public function getTypeMaster($param, $data, $field = 'type_id')
	{
		$data = self::disposeCourseType($data, $field);

		if (!$data) return [];

		$user = user();

		$points = $data[$param[$field]] ? returnCourseSumField($data[$param[$field]]) : 0;

		$userLogs = KnowUserLog::getUserCount([$field => $param[$field]], 'column');

		$userPoints = $user ?  array_sum($userLogs) : 0;

		$counts = $data[$param[$field]] ? count($data[$param[$field]]) : 0;

		$userCounts = $user ? count($userLogs) : 0;

		$userTypes = $user ? KnowUserType::getUserData($param['type_id'], 'count', 'know_user_type.type_id') : 0;

		return [
			'counts' => $counts,
			'points' => $points,
			'userTypes' => $userTypes,
			'userCounts' => $userCounts,
			'userPoints' => $userPoints,
		];
	}

	public function disposeCourseSpec($param, $data)
	{
		$user = user();

		$courseData = [];

		$sData = Specific::getWhereData($param);

		list($data, $courseData) = self::disposeCourseLog($param, $data);

		foreach($sData as $key => &$v)
		{
			$v['course'] = $courseData[$v['s_id']];
		}

		return $sData;
	}

	public function disposeCourseLog($param, $data, $status = 's')
	{
		$user = user();

		$courseData = [];

		$logs = $user ? KnowUserLog::getUserCount($param, 'column', 'course.course_id,know_user_log.points') : [];

		$records = $user ? KnowUserRecord::getUserCount($param, 'column', 'know_user_record.course_id,record') : [];

		$courseIds = Course::getCourseIds($param['type_id'], 'course.type_id', 2);

		foreach ($data as $key => &$v)
		{
			$v['status'] = $logs[$v['course_id']] ? 1 : 0;

			$points = $logs ? $logs[$v['course_id']] : 0;

			$record = $v['type'] == 1 ? $records[$v['course_id']] : 0;

			$v['percent'] = $v['status'] == 1 ? ($v['type'] != 3 ? ($v['type'] == 1 ? ($v['video_time'] ? round(($record/$v['video_time'])*100) : 0) : 100) : round($points/$v['points']*100)) : 0;

			$v['is_free'] = in_array($v['course_id'], $courseIds) ? 1 : 0;

			if ($status == 's') $courseData[$v['s_id']][] = $v;
		}

		return [ $data, $courseData ];
	}

	public function getPrevNextData($course, $status = 'next')
	{
		$courseIds = Course::getCourseIds($course['s_id'], 'course.s_id');

		$thisId = array_search($course['course_id'],$courseIds);

		if ( $status == 'next')
		{
			if ($thisId == (count($courseIds)-1))
			{
				$sIds = Specific::getSIds($course['type_id'], 'type_id');

				$thisSId = array_search($course['s_id'],$sIds);

				if ( $thisSId == (count($sIds)-1) )
				{
					$data = [];
				}
				else
				{
					$courseIds = Course::getCourseIds($sIds[$thisSId+1], 'course.s_id');

					$data = $courseIds ? Course::getDataOne($courseIds[0]) : [];
				}
			}
			else
			{
				$data = Course::getDataOne($courseIds[($thisId+1)]);
			}
		}
		else
		{
			$data = $thisId == 0 ? [] : Course::getDataOne($courseIds[($thisId-1)]);
		}

		return $data;
	}


	public function getSPrevNextData($param)
	{
		$sData = Specific::getOneData($param['sid']);

		$sIds = Specific::getSIds($sData['type_id'], 'type_id');

		$thisId = array_search($param['sid'],$sIds);

		if ( $param['status'] == 'prev')
		{
			$thisId = $thisId == 0 ? count($sIds)-1 : $thisId-1;
		}
		else
		{
			$thisId = $thisId == (count($sIds)-1) ? 0 : $thisId+1;
		}

		$sId = $sIds[$thisId];

		$data = Course::getWhereData(['s_id' => $sId]);

		list($courseData, $arr) = self::disposeCourseLog(['s_id' => $sId], $data);

		$sData = Specific::getOneData($sId);

		return [$courseData,$sData];
	}


	public function disposeCourseType($data, $field)
	{
		$courseData = [];

		foreach ($data as $key => $v)
		{
			$courseData[$v[$field]][] = $v;
		}

		return $courseData;
	}


	public function getCourseDetail($id)
	{
		$data = Course::getDataOne($id);

		if (!$data) return [];

		$data['status'] = VipService::getUserVip(['id'=>$data['nav_id'],'type_id'=>$data['type_id']]);

		if ($data['status'] == 0)
		{
			$courseIds = Course::getCourseIds($data['type_id'], 'course.type_id', 2);

			if (in_array($data['course_id'], $courseIds))

				$data['status'] = 2;
		}

		if ( $data['status'] == 0 && $data['s_free'] == 1) $data['status'] = 2;

		Course::where('course_id', $id)->setInc('visits');

		return $data;
	}


	public function getNavPrice($data, $pack = 2)
	{
		$package = NavPackage::getOneData($data['nav_id'], $pack);

		if (!$package) return [];

		$data['nav_price'] = $package['price'];

		$data['original_price'] = $package['original_price'];

		return $data;
	}

	public function getQuesLists( $param )
	{
		$data = KnowUserQuestion::getCourseUserQues($param);

		foreach($data as $key => &$v)
		{
			$v['answer'] = $v['answer'] ? KnowUserQuestion::getCourseUserQuesAnswer($v['id']) : [];
		}

		$paginator = new Paginator(5, $param['page'] ?: 1, $data->total());

        $render = $paginator->render();

		return [ $data, $render ];
	}
}